{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=210
  Verdict: Accepted
}
uses
	SysUtils, Math; 
 
var
	i,n:longint;
	a,d:array[0..110] of longint;
 
procedure swap(var a,b:longint);
 
var
	t:longint;
 
begin
t:=a;
a:=b;
b:=t;
end;
 
procedure sort(l,r:longint);
 
var
	i,j,x:longint;
 
begin
i:=l;
j:=r;
x:=a[(l+r) shr 1];
repeat
	while a[i] < x do inc(i);
	while a[j] > x do dec(j);
	if i <= j then begin
		swap(a[i],a[j]);
		inc(i);
		dec(j);
	end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n);
for i:=1 to n do
	read(A[i]);
sort(1,n);
d[0]:=0;
d[1]:=maxlongint;
for i:=2 to n do 
	d[i]:=min(d[i-1], d[i-2]) + a[i] - a[i - 1];
write(d[n]);
end.